part of shop;

class DetailGoodsPage extends StatefulWidget {
  final String? id;
  final Map? pagemap;
  // late Map<String, dynamic> infomap;

  DetailGoodsPage({Key? key, this.id, this.pagemap}) : super(key: key);

  //传入的id用于进行查找商品的详细数据；
  @override
  State<DetailGoodsPage> createState() => _DetailGoodsPageState();
}

class _DetailGoodsPageState extends State<DetailGoodsPage> {
  final UserPref infomap=tempUser();
  @override
  Widget build(BuildContext context) {
    ///TODO 先写死不报错，好改外观
    // return FutureBuilder(
    //     future: User._cache?.getLocalInfo().then((value) {
    //       widget.infomap = json.decode(value) as Map<String, dynamic>;
    //     }),
    //     builder: (BuildContext context,
    //         AsyncSnapshot<Map<dynamic, dynamic>?> snapshot) {
          return Scaffold(
              appBar: AppBar(
                  //leading: null,
                  automaticallyImplyLeading: true,
                  title: const Text('商品'),
                  actions: <Widget>[
                    IconButton(
                      icon: const Text("举报"),
                      onPressed: () {
                        Navigator.of(context)
                            .push(MaterialPageRoute(builder: (context) {
                          return Scaffold(
                            appBar: AppBar(
                              title: const Text("举报"),
                            ),
                            body: const Center(
                              child: Text("请线下解决"),
                            ),
                          );
                        }));
                      },
                    )
                  ]),
              body:
              // Material(child:
                  CustomScrollView(
                      scrollDirection: Axis.vertical,
                      slivers: <Widget>[
                    SliverList(
                        delegate: SliverChildListDelegate([
                      Container(
                        decoration: const BoxDecoration(color: Colors.white),
                        padding: const EdgeInsets.fromLTRB(30, 15, 30, 0),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Row(children: <Widget>[
                              Container(
                                padding: const EdgeInsets.fromLTRB(0, 0, 25, 0),
                                child: const Userpicturedemo(
                                  width: 65,
                                  height: 65,
                                ),
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Row(
                                    children: <Widget>[
                                      Text(
                                        "商品名?",
                                        // widget.infomap[""],
                                        style: const TextStyle(
                                          fontSize: 23,
                                          fontWeight: FontWeight.w800,
                                          color: Colors.white,
                                        ),
                                      ),
                                      const SizedBox(
                                        width: 12,
                                      ),
                                      Container(
                                        child: const Text("已认证",
                                            textAlign: TextAlign.center,
                                            style: TextStyle(
                                                wordSpacing: 0.1,
                                                color: Colors.white)),
                                        height: 20,
                                        width: 80,
                                        decoration: BoxDecoration(
                                            color: const Color.fromRGBO(
                                                111, 207, 151, 1),
                                            border: Border.all(
                                              color: const Color.fromRGBO(
                                                  111, 207, 151, 1),
                                              width: 1.0,
                                            ),
                                            borderRadius:
                                                const BorderRadius.all(
                                                    Radius.circular(4.0))),
                                      )
                                    ],
                                  ),
                                  Text(infomap.detail.dormitory,
                                      style: const TextStyle(
                                          fontSize: 13,
                                          color:
                                              Color.fromRGBO(93, 94, 89, 1))),
                                  usersellnumber(
                                    sellnumber: 1,
                                  ),
                                ],
                              )
                            ]),
                            const Divider(
                              height: 10,
                            ),
                            prisetab(
                                double.parse(widget.pagemap?["product_price"])),
                            const Divider(
                              height: 8,
                            ),
                            Text(
                              widget.pagemap?["product_intro"],
                              style: const TextStyle(
                                  fontSize: 20, fontWeight: FontWeight.w800),
                            ),
                            const Divider(),
                            Container(
                              alignment: Alignment.topCenter,
                              //height: 400,
                              child: Image.network(
                                  widget.pagemap?["product_coverimg"][0]),
                            ),
                            ButtonBar(
                              children: [
                                ElevatedButton(
                                    onPressed: _updateProductLiked,
                                    child: const Text("收藏")),
                                ElevatedButton(
                                    onPressed: () {
                                      Navigator.push(context,
                                          MaterialPageRoute(builder: (context) {
                                        return DetailGoodsPage(
                                            pagemap: widget.pagemap);
                                      }));
                                    },
                                    child: const Text("购买")),
                              ],
                            ),
                            const Divider(),
                            ListTile(
                              leading: Image.asset("images/qqIcon.jpeg"),
                              title: const Text(
                                "联系方式",
                                textAlign: TextAlign.center,
                              ),
                              subtitle: TextButton(
                                child: const Text(
                                  "2019631918",
                                ),
                                onPressed: () async {
                                  ///根据卖方提供的qq号跳转，这里为了测试暂时写死
                                  bool res = await jump("2019631918");
                                  if (res == false) {
                                    showDialog(
                                        builder: (BuildContext context) =>
                                            ShowDefineAlertWidget(
                                                () {}, "跳转失败", "您还未安装QQ"),
                                        context: context);
                                  }
                                },
                              ),
                            )
                          ],
                        ),
                      ),
                    ]))
                  ])
          );
  }

  _updateProductLiked() async {
    //=List<Map>.generate(10,);
    //list使用前要设置初始长度。。。。。。。。。。。。。
    List<Map> likedListMap;
    likedListMap = [];
    List<String> liked = [];
    //初始化
    SharedPreferences uPrefs = await SharedPreferences.getInstance();
    if (!uPrefs.containsKey("Liked")) {
      uPrefs.setStringList("Liked", liked);
    }
    liked = uPrefs.getStringList("Liked")!;
    for (int i = 0; i < liked.length; i++) {
      likedListMap.add(json.decode(liked[i]));
    }
    //初始化
    //检查是否重复
    bool isRepetitive = false;
    for (int i = 0; i < likedListMap.length; i++) {
      if (likedListMap[i]["product_id"] == widget.pagemap?["product_id"]) {
        isRepetitive = true;
        break;
      }
    }
    //重复则弹出通知
    if (isRepetitive) {
      AppTool().showDefineAlert(context, null, "温馨提示", '您已经收藏过该商品了');
    } else {
      //否则
      //达到上限也弹出通知
      if (liked.length == 10) {
        AppTool().showDefineAlert(context, null, "温馨提示", '您可收藏的商品数已达到上限');
      } else {
        //把这个商品的数据以String形式传入
        liked.add(json.encode(widget.pagemap));
        //将新的数组传到shared_preference中
        uPrefs.setStringList("Liked", liked);
      }
    }
    //await writeLocalString(widget.pagemap.toString());
    //await readLocalString().then((value) => print(value));
  }
  //);
  //;
  //}

}

///临时调试用户
tempUser(){
  var ud = UserDetail();
  ud
    ..dormitory = "3"
    ..fullName = "spider"
    ..introduce = "dsds";
  var up = UserPref();
  up
    ..name = "spider"
    ..detail = ud
    ..avatar =
        'https://cdn.nlark.com/yuque/0/2022/jpeg/23159950/1664536788207-4ca31a99-e010-49c3-b50c-1f3feaf5c37e.jpeg'
    ..id = "sdsdsdss"
    ..phone = "12345678901";
  return up;
}
